/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.Message;
//import org.crystalrange.enterprise.model.Document;
import org.crystalrange.enterprise.model.Material;
import org.crystalrange.enterprise.service.MaterialService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class MaterialController extends BaseController {

    protected MaterialService materialService;
    protected Material material;
    protected Listbox listboxDocument;

    public MaterialService getMaterialService() {
        return this.materialService;
    }

    public void setMaterialService(MaterialService materialService) {
        this.materialService = materialService;
    }

    public Material getMaterial() {
        return this.material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.material = this.materialService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Material");
        } else {
            this.material = this.materialService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Material");
        }

        component.setAttribute("controller", this, true);
    }

    public void onClick$menuitemMaterialSave(Event event) throws Exception {
        this.materialSave();
    }

    public void onClick$toolbarbuttonMaterialSave(Event event) throws Exception {
        this.materialSave();
    }

    public void onClick$toolbarbuttonDocumentAdd(Event event) throws Exception {
        this.documentAdd();
    }

    public void onClick$menuitemDocumentAdd(Event event) throws Exception {
        this.documentAdd();
    }

    public void onClick$toolbarbuttonDocumentRemove(Event event) throws Exception {
        this.documentRemove();
    }

    public void onClick$menuitemDocumentRemove(Event event) throws Exception {
        this.documentRemove();
    }

    private void materialSave() throws Exception {
        this.materialService.save(this.material,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }

    private void documentAdd() throws Exception {
   /*     Window window = (Window) this.execution.createComponents("~./zul/html/documentSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Document selectedDocument = (Document) this.execution.getAttribute("selectedDocument");
            this.material.getDocuments().add(selectedDocument);
        }

        this.binder.loadComponent(this.listboxDocument);*/
    }

    private void documentRemove() throws Exception {
     /*   Set<Listitem> selectedItems = this.listboxDocument.getSelectedItems();

        for (Listitem selectedItem : selectedItems) {
            Document selectedDocument = (Document) selectedItem.getValue();
            this.material.getDocuments().remove(selectedDocument);
        }

        this.binder.loadComponent(this.listboxDocument);*/
    }
}
